﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;
using System.Windows.Interop;
using System.Windows.Media.Media3D;
using System.Windows.Media.Animation;
using System.IO;
using System.Runtime.InteropServices;
using System.Data;
using System.Windows.Threading;
using System.Windows.Forms;

namespace FaceCat
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        /// <summary>
        /// 创建窗体
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();
            m_uiView = new FCUIView();
            Grid1.Children.Add(m_uiView);
            m_uiView.onLoad();
            m_mainFrame = new MainFrame();
            m_mainFrame.setNative(m_uiView.m_native);
            m_mainFrame.loadFile(System.Windows.Forms.Application.StartupPath + "\\config\\MainFrame.html", null);
            InputMethod.SetIsInputMethodEnabled(this, true);
            InputMethod.SetIsInputMethodSuspended(this, true);
            this.SourceInitialized += new EventHandler(MainWindow_SourceInitialized);
        }

        private MainFrame m_mainFrame;

        /// <summary>
        /// 绘图对象
        /// </summary>
        private FCUIView m_uiView;

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void MainWindow_SourceInitialized(object sender, EventArgs e)
        {
            IntPtr hwnd = ((HwndSource)PresentationSource.FromVisual(m_uiView)).Handle;
            HwndSource.FromHwnd(hwnd).AddHook(new HwndSourceHook(WndProc));
            m_uiView.m_host.setHWnd(hwnd);
        }

        /// <summary>
        /// Windows消息
        /// </summary>
        public IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            if (m_uiView.m_host.onMessage(hwnd, msg, wParam, lParam) > 0)
            {
                handled = true;
            }
            return IntPtr.Zero;
        }

        /// <summary>
        /// 窗体关闭方法
        /// </summary>
        /// <param name="e">参数</param>
        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);
            Process.GetCurrentProcess().Kill();
        }

        /// <summary>
        /// 大小改变方法
        /// </summary>
        /// <param name="sizeInfo">参数</param>
        protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
        {
            base.OnRenderSizeChanged(sizeInfo);
            m_uiView.Height = sizeInfo.NewSize.Height - 30;
        }
    }
}
